package ru.poomsae.isap.Online.SystemCut

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Vibrator
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.example.isap_wtf.Model.ApiEndPoint
import com.example.isap_wtf.Model.ParametrsISAP
import com.example.isap_wtf.Model.zaprosJSON
import ru.poomsae.isap.Offline.MenuActivity
import ru.poomsae.isap.R


class SystemOfCut : AppCompatActivity() {

    var min03: Int = 0
    var min01: Int = 0
    var accyracy: Int = 40
    var accyracyDouble: Double = 4.0
    var onlineConSys = ParametrsISAP.onlineConSys
    lateinit var vibrator: Vibrator
    var mIDHost: String = ParametrsISAP.idHostServer
    var url = ""
    var idvistuplenie = "null"
    lateinit var myTextView: TextView
    lateinit var myLinearLayout: LinearLayout
    lateinit var myRelativeLayout: RelativeLayout
    lateinit var mySudTextView: TextView
    lateinit var myPoomsaeTextView: TextView
    lateinit var myzaprosJSON: zaprosJSON

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.sys_of_cut)

        url = "http://" + mIDHost + ApiEndPoint.VISTUPLENIEA

        myTextView = findViewById(R.id.idvistText) as TextView
        println("myTextView")
        myLinearLayout = findViewById(R.id.progressBar) as LinearLayout
        println("myLinearLayout")
        myRelativeLayout = findViewById(R.id.system_of_cut) as RelativeLayout
        println("myRelativeLayout")
        mySudTextView = findViewById(R.id.sudNumTV) as TextView
        println("mySudTextView")
        myPoomsaeTextView = findViewById(R.id.phumTV) as TextView
        println("myPoomsaeTextView")

        myzaprosJSON = zaprosJSON(myTextView, myLinearLayout, myRelativeLayout, mySudTextView, myPoomsaeTextView)
        val decorView = window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN

        if(onlineConSys == 1) {
            myzaprosJSON.waitVistup()
            visibleSystem()
            decorView.systemUiVisibility = uiOptions
            getSupportActionBar()?.hide()
        }

        if (onlineConSys == 0) {
            mySudTextView.visibility = View.GONE
            myPoomsaeTextView.visibility = View.GONE
        }

        val btn03 = findViewById<Button>(R.id.btn03)
        val btn01 = findViewById<Button>(R.id.btn01)
        val btnRes = findViewById<Button>(R.id.btnRes)
        val btnCan03 = findViewById<Button>(R.id.btnCan03)
        val btnCan01 = findViewById<Button>(R.id.btnCan01)
        val btnPresentation = findViewById<Button>(R.id.btnPresentation)
        val tView03 = findViewById<TextView>(R.id.tView03)
        val tView01 = findViewById<TextView>(R.id.tView01)
        vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        btn03.setOnClickListener { if (accyracy <= 40 && accyracy > 0) { vibrator.vibrate(200); min03 = min03 + 1; scoring() } }
        btn01.setOnClickListener {  if (accyracy <= 40 && accyracy > 0) { vibrator.vibrate(200); min01 = min01 + 1; scoring() } }
        btnRes.setOnClickListener { min03 = 0; min01 = 0; accyracy = 40; scoring(); tView03.setText(min03.toString() + " x "); tView01.setText(" x " + min01.toString()) }
        btnCan03.setOnClickListener { if (accyracy < 40 && min03 > 0) {min03 = min03 - 1; scoring(); tView03.setText(min03.toString() + " x ")} }
        btnCan01.setOnClickListener { if (accyracy < 40 && min01 > 0) {min01 = min01 - 1; scoring(); tView01.setText(" x " + min01.toString())} }
        btnPresentation.setOnClickListener {
            val intent = Intent (this, PresentationActivity::class.java)
            intent.putExtra("accuracyDouble", accyracyDouble)
            startActivity(intent)
        }
    }

    fun scoring() {
        val tView03 = findViewById<TextView>(R.id.tView03)
        val tView01 = findViewById<TextView>(R.id.tView01)
        val tvAccyrasy = findViewById<TextView>(R.id.tvAccyrasy)
            accyracy = 40 - min01 - min03*3
            accyracyDouble = accyracy.toDouble()/10
            tvAccyrasy.setText(accyracyDouble.toString())
            tView01.setText(" x " + min01.toString())
            tView03.setText(min03.toString() + " x ")

        if (accyracy < 0) {
            accyracyDouble = 0.0
            tvAccyrasy.setText(accyracyDouble.toString())
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_off, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item?.itemId) {
            R.id.menu_offline -> {
                val intent = Intent (this, MenuActivity::class.java)
                startActivity(intent)
            }

        }

        return true
    }

    @SuppressLint("WrongViewCast")
    fun visibleSystem() {

        if (idvistuplenie == "null") {
            val system_of_cut = findViewById<RelativeLayout>(R.id.system_of_cut)
            val progressBar = findViewById<LinearLayout>(R.id.progressBar)
            val idvistText = findViewById<TextView>(R.id.idvistText)
            system_of_cut.visibility = View.GONE
            progressBar.visibility = View.VISIBLE
            idvistText.setText(getString(R.string.idvistTextView))
        }
    }

    override fun onBackPressed() {
        //Наше поведение...
    }

}




